package com.bjc.principle.demeter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @program: DesignPattern
 * @description: 迪米特原则
 * @author: bjc
 * @create: 2020-12-13 18:02
 */
//客户端[使用者]
public class Demeter1 {

    public static void main(String[] args) {
        SchoolManager schoolManger = new SchoolManager();
        schoolManger.printAllEmployee(new CollegeManager());
    }
}

//学校总部员工类
class Employee{
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

//学院员工类
class CollegeEmployee{
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

//管理学院员工的管理类
class CollegeManager{
    //返回学院的所有员工
    public List<CollegeEmployee> getAllEmployee(){
        List<CollegeEmployee> list = new ArrayList<CollegeEmployee>();
        for (int i = 0; i<10; i++) {
            CollegeEmployee emp = new CollegeEmployee();
            emp.setId("学院员工id="+i);
            list.add(emp);
        }
        return list;
    }
}

//学校管理类
//分析SchoolManager类的直接朋友类有哪些Employee,CollegeManager
//CollegeEmployee 不是直接朋友 这样违反了迪米特原则
class SchoolManager{
    //返回学校总部的员工
    public List<Employee> getAllEmployee(){
        List<Employee> list = new ArrayList<Employee>();
        for (int i = 0; i < 5; i++) {
            Employee emp = new Employee();
            emp.setId("学院总部员工id="+i);
            list.add(emp);
        }
        return list;
    }

    //该方法完成输出学校总部和学院员工信息（id）
    void printAllEmployee(CollegeManager sub){
        //分析问题
        //1.这里CollegeEmployee不是SchoolManager的直接朋友
        //2.CollegeEmployee是以局部变量的方式出现在SchoolManager
        //3.违反了迪米特法则
        List<CollegeEmployee> list1 = sub.getAllEmployee();
        System.out.println("-------------学院员工--------------");
        for (CollegeEmployee e : list1) {
            System.out.println(e.getId());
        }
        List<Employee> list2 = this.getAllEmployee();
        System.out.println("-------------学校总部员工--------------");
        for (Employee e : list2) {
            System.out.println(e.getId());
        }
    }
}